
class SumFilter:
    """
    Object used for data validation.
    """
    
    def __init__(self, method, bufferSize = 8096):
        """
        @param method: the method used to check data.
        """
        try:
            self._mod = __import__("checker." + method)
            self._method = method
        except ImportError:
            self._mod = __import__("checker.haohaoxiansheng")
            self._method = "haohaoxiansheng"         
        self._mod = getattr(self._mod, self._method)
        self._checkerClass = getattr(self._mod, getattr(self._mod, 'name'))
        self._checker = self._checkerClass()
        self._checkedSize = 0
        self._bufferSize = bufferSize
        
    def filter(self, infile, size):
        checksize = size
        self._checker.reset()
        while checksize > 0:
            data = infile.read(checksize > self._bufferSize and self._bufferSize or checksize)
            if not data: break
            checksize -= len(data)
            self._checker.update(data)
        return self._checker.digest()
        
    
#    
#
#if __name__ == "__main__":
#    from stream.binarystream import BinaryStream
#    f = BinaryStream("f:/PPA401.rar", True)
#    f.open()
#    #print(f.getSize())
#    
#    sf = SumFilter("md5")
#    
#    print(sf.filter(f,f.getSize()))
#    
#    
#    
#    #chk = SumFilter("md5")
    #print(chk.filter(b"Kenneth Tse"))
    #print(chk.filter(b"Kenneth Tse"))
    